package cmpe273.classproject.server.dao;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Random;

import cmpe273.classproject.server.model.Movie;

public class Cache {

	private Map<Integer, Object> map;
	private int maxSize;
	private int size;

	private static Cache instance = null;

	private Cache() {
		map = new LinkedHashMap<Integer, Object>(100000);
		this.maxSize = 100000;
	}

	public static Cache getInstance() {
		if (instance == null) {
			instance = new Cache();
		}
		return instance;
	}

	public void put(Integer key, Object value) {
		// Check if map is full
		if (size + 1 < maxSize) {
			map.put(key, value);
			size++;
		}// If full then insert at random position
		else {
			Random rnd = new Random();
			int newKey = rnd.nextInt(maxSize);
			map.put(newKey, value);
		}
	}

	public Object get(Integer key) {
		return map.get(key);
	}

	public void remove(Integer key) {
		map.remove(key);
	}
}
